package com.weic.easycache.framework;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;

import com.weic.easycache.Const;
import com.weic.easycache.framework.anno.Injector;

public abstract class AbsActivity extends FragmentActivity {

	@Override
	public void startActivity(Intent intent) {
		startActivityForResult(intent, Const.REQUEST_CODE_NORMAL);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		switch (requestCode) {
		case Const.REQUEST_CODE_NORMAL:
			//什么也不做，确保回调onActivityResult()以执行可能的退出操作
			break;
		}

		//退出应用，无论是否退出，都应该首先执行上面的代码，确保数据不丢失
		if(resultCode == Const.RESULT_CODE_EXIT) {
			exsit(data == null ? null : new Intent().putExtras(data).setData(data.getData()));
		}else if(data != null) {
			String name = data.getStringExtra(Const.EXTRA_BACK_TO_NAME);
			if(name != null) {
				if(name.equals(getClass().getSimpleName())) {
					data.removeExtra(Const.EXTRA_BACK_TO_NAME);
					return;
				}
				backTo(resultCode, name, data == null ? null : new Intent().putExtras(data).setData(data.getData()));
			}else {
				int count = data.getIntExtra(Const.EXTRA_BACK_TO_COUNT, -1);
				if(--count > 0) {
					backTo(resultCode, count, data == null ? null : new Intent().putExtras(data).setData(data.getData()));
				}else {
					data.removeExtra(Const.EXTRA_BACK_TO_COUNT);
					return;
				}
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Injector.inject(this, this, AbsActivity.class);
	}

	//递归循环退出整个应用
	public void exsit() {
		exsit(null);
	}

	public void exsit(Intent data) {
		setResult(Const.RESULT_CODE_EXIT, data);
		finish();
	}

	public void backTo(int resultCode, String activityName) {
		backTo(resultCode, activityName, null);
	}

	public void backTo(int resultCode, String activityName, Intent data) {
		setResult(resultCode, data == null ?
				new Intent().putExtra(Const.EXTRA_BACK_TO_NAME, activityName) :
					data.putExtra(Const.EXTRA_BACK_TO_NAME, activityName));
		finish();
	}

	public void backTo(int resultCode, int count, Intent data) {
		setResult(resultCode, data == null ?
				new Intent().putExtra(Const.EXTRA_BACK_TO_COUNT, count) :
					data.putExtra(Const.EXTRA_BACK_TO_COUNT, count));
		finish();
	}

	@Override
	public void setContentView(int layoutResID) {
		super.setContentView(layoutResID);
		initClickBlankAreaHandler(((ViewGroup)getWindow().getDecorView()).getChildAt(0));
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(view);
		initClickBlankAreaHandler(view);
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		super.setContentView(view, params);
		initClickBlankAreaHandler(view);
	}

	protected int[] getClickHideInputMethodViewIds() {
		return null;
	}

	private void initClickBlankAreaHandler(View rootView) {
		rootView.setOnClickListener(mOnClickBlankAreaListener);
		int[] ids = getClickHideInputMethodViewIds();
		if(ids != null) {
			View view;
			for(int id : ids) {
				view = rootView.findViewById(id);
				if(view != null) view.setOnClickListener(mOnClickBlankAreaListener);
			}
		}
	}

	private View.OnClickListener mOnClickBlankAreaListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			hideInputMethod();
		}
	};

	protected void showInputMethod() {
		View focusView = getCurrentFocus();
		if(focusView != null) { //是否存在焦点
			InputMethodManager inputMethodManager = (InputMethodManager)
					getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethodManager.showSoftInput(focusView, InputMethodManager.SHOW_IMPLICIT);
		}
	}

	protected void hideInputMethod() {
		IBinder windowToken = getWindow().getDecorView().getWindowToken();
		if(windowToken != null) {
			InputMethodManager inputMethodManager = (InputMethodManager)
					getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethodManager.hideSoftInputFromWindow(
					windowToken, InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}
}
